<?php

namespace app\index\controller;

use app\admin\model\Shop;
use app\index\model\Lunbo;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Lunbos extends Controller{
  public function lunbo(){
        $lunbo = new Lunbo();
        $shop = new Shop();
        return [$lunbo::all(),$shop::all()];

  }
  public function leibie(){
        $data = Db::query('select * from leibie');
        return $data;
  }
  public function xifa($id){
      $data = Db::query('select xifen.id,xifen.name,xifen.jiage,xifen.img,xifen.xifen_id from xifen join leibie on xifen_id = leibie.id WHERE leibie.id=?',[$id]);
      return $data;
  }
  public function goumai($id){
      $data = Db::query("select * from shop WHERE id=$id");
      return $data;
  }
  public function tianjia($id){
      $se = Session::get('user');
      $a = $se[0]['user_id'];
      $data1 = Db::query('select * from gouwuche1 WHERE users_id=? and shops_id=?',[$a,$id]);
      $data2 = Db::query('select xifen_id from xifen WHERE id=?',[$id]);
      if (empty($data1)){
          $data = Db::execute("insert into gouwuche1(users_id,shops_id) VALUES (?,?)",[$a,$id]);
          if($data==1){
              $a = '添加购物车成功';
              return [$a,$data2];
          }
      }else{
          $b = "此物品已在你的购物车";
         return [$b,$data2];
      }

  }
  public function gouwuche(){
      $se = Session::get('user');
      $a = $se[0]['user_id'];
//      $data = Db::query('select shops_id from gouwuche1 WHERE users_id=?',[$a]);
//      $data1 = Db::query('select * from gouwuche1 join xifen on shops_id=xifen.id where users_id=?',[$a]);
      $data2 = Db::query('select xifen.jiage from gouwuche1 join xifen on shops_id=xifen.id where users_id=?',[$a]);
      $data4 = Db::query("select * from gouwuche JOIN shop ON shops_id=shop.id WHERE users_id=$a");
      $data5 = Db::query('select shop.price from gouwuche join shop on shops_id=shop.id where users_id=?',[$a]);
      $data1=Db::query("SELECT *,count(*) as shuliang FROM gouwuche1 JOIN xifen ON shops_id=xifen.id WHERE users_id=$a GROUP BY shops_id");
      $data6 =0;
      for($b=0;$b<sizeof($data5);$b++){
          $data6 += $data5[$b]['price'];
      }
      $data3 = 0;
      for($b=0;$b<sizeof($data2);$b++){
          $data3 += $data2[$b]['jiage'];
      }
      $data7 = $data6+$data3;
      return [$data1,$data7,$data4];
  }

    public function tejia($id){
        $se = Session::get('user');
        $a = $se[0]['user_id'];
        $data1 = Db::query('select * from gouwuche WHERE users_id=? and shops_id=?', [$a, $id]);
        if (empty($data1)) {
            $data = Db::execute("insert into gouwuche(users_id,shops_id) VALUES (?,?)", [$a, $id]);
            if ($data == 1) {
                $a = '添加购物车成功';
                return $a;
            }
        } else {
            $b = "此物品已在你的购物车";
            return $b;
        }
    }
    //单个产品添加
    function add(){
       $id=input('id');
       $user=Session::get('user');
       $uid=$user[0]['id'];
       $data=Db::execute("INSERT  INTO  gouwuche1 (users_id, shops_id)VALUES ($uid,$id) ");
       if($data==0){
           return['status'=>false,'error'=>"插入错误!"];
       }else{
           return['status'=>true,'msg'=>'添加成功!'];
       }
    }


}
